package com.cema.manage.common.exception;

import com.cema.manage.project.android.result.JsonResult;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.exceptions.TemplateInputException;

@ControllerAdvice
public class CommonExceptionHandler {

    /**
     * 处理全局的 GlobalException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = DemoModeException.class)
    @ResponseBody
    public JsonResult handlerGlobalException(DemoModeException e) {
        return JsonResult.returnFail(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonResult handlerGlobalException(Exception e) {
        e.printStackTrace();
        return JsonResult.returnFail("Fail" + e.getMessage());
    }

    @ExceptionHandler(value = AuthorizationException.class)
    public String handlerAuthorizationException(Exception e) {
        return "error/unauth";
    }

    @ExceptionHandler(value = TemplateInputException.class)
    public String handlerTemplateInputException(Exception e) {
        return "error/unauth";
    }

    @ExceptionHandler(value = UnauthenticatedException.class)
    public String handlerTemplateUnauthenticatedException(Exception e) {
        return "error/unauth";
    }
}